package com.lee.dhsg.dao.provider;

import com.lee.dhsg.domain.Order;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

/**
 * @author Jun
 */
public class OrderSqlProvider {

    /**
     * 动态根据（课程和学生）查询数量
     * @param params
     * @return
     */
    public String count(Map<String,Object> params){
        return new SQL(){
            {
                SELECT("count(*)");
                FROM("`order`");
                if (params.get("order")!=null){
                    Order order=(Order) params.get("order");
                    if (order.getCourse()!=null&&order.getCourse().getCourseid()!=null&&order.getCourse().getCourseid()!=0){
                        WHERE("coursid=#{order.course.courseid}");
                    }
                    if(order.getStudent()!=null&&order.getStudent().getSid()!=null&&order.getStudent().getSid()!=0){
                        WHERE("sid=#{order.student.sid}");
                    }
                }
            }
        }.toString();
    }

    /**
     * 动态查询订单
     * @param params
     * @return
     */
    public String selectWhitParam(Map<String,Object> params){
        return new SQL(){
            {
                SELECT("*");
                FROM("`order`");
                if (params.get("order")!=null){
                    Order order=(Order) params.get("order");
                    if (order.getCourse()!=null&&order.getCourse().getCourseid()!=null&&order.getCourse().getCourseid()!=0){
                        WHERE("coursid=#{order.course.courseid}");
                    }
                    if(order.getStudent()!=null&&order.getStudent().getSid()!=null&&order.getStudent().getSid()!=0){
                        WHERE("sid=#{order.student.sid}");
                    }
                    if (order.getPrice()!=null&&!order.getPrice().equals("")){
                        WHERE("price=#{order.price}");
                    }
                    if(order.getTime()!=null&&!order.getPrice().equals("")){
                        WHERE("time=#{order.time}");
                    }
                }
            }
        }.toString();
    }

    /**
     * 动态插入订单
     * @param params
     * @return
     */
    public String insertOrder(Map<String,Object> params){
        return new SQL(){
            {
                INSERT_INTO("`order`");
                if (params.get("order") != null) {
                    Order order=(Order)params.get("order");
                    if (order.getCourse().getCourseid() != null) {
                        VALUES("courseid", "#{order.course.courseid}");
                    }
                    if (order.getCourse().getDiscountPrice() != null) {
                        VALUES("price", "#{order.course.discountPrice}");
                    }
                    if (order.getCourse().getDiscountPrice()==null){
                        VALUES("price","#{order.course.originaPrice}");
                    }
                }
                if(params.get("sid")!=null){
                    VALUES("sid","#{sid}");
                }
                if(params.get("time")!=null){
                    VALUES("time","#{time}");
                }
            }
        }.toString();
    }

}
